<template>
	<view class="content">
		<u-navbar bgColor="#fff" :placeholder="true" :fixed="true" @leftClick="$back" title="帮助反馈" titleStyle="{'color': '#000'}">
			
		</u-navbar>
		<view class="baibox">
			<view class="title">
				遇到问题
			</view>
			<input type="text" class="put" placeholder-class="font-gray" placeholder="请输入" v-model="title">
			<view class="title">
				描述详情
			</view>
			<input type="text" class="put" placeholder-class="font-gray" placeholder="请填写详细情况" v-model="desc">

			<view class="title pb20">
				凭证图片
				<text>{{images.length}}/8</text>
			</view>
			<view class="imgView">
				<!-- <view v-for="(img,index) in images" class="imgList">
					<image :src="img | formatImgUrl" mode="aspectFill" style="width: 190rpx;height: 190rpx;"></image>
					<view class="imgClose" @click="deletePic(index)">×</view>
				</view>
				<image :src="'/images/up.png' | formatImgUrl" mode="widthFix" style="width: 190rpx;height: 190rpx;"
					@click="onChooseImg" v-if="images.length < 8"></image> -->
					
				<u-upload ref="uUpload" :uploadIcon="'/images/up.png' | formatImgUrl" :multiple="true" maxCount="8" width="78" height="78" :action="uploadImgUrl" :fileList="images" @afterRead='afterRead' @delete="deletePic">
				</u-upload>
			</view>
		</view>
		<view class="subbox" @click="submit">
			提交
		</view>
	</view>
</template>
<script>
	import * as db from '@/common/db.js'
	export default {
		data() {
			return {
				images: [],
				title: '',
				desc: '',
				fileList2: []
			}
		},
		onLoad(e) {
				let auth = db.get('auth');
				if (!auth) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}
				
		},
		methods: {
			// 提交 意见反馈
			submit() {
				
				
				
				if (!this.title) {
					this.$common.errorToShow('请输入遇到问题')
					return
				}
				if (!this.desc) {
					this.$common.errorToShow('请填写详细情况')
					return
				}

				this.$api.default.request('user/feedbackUser', {
					title: this.title,
					desc: this.desc,
					images: this.fileList2
				}, 'POST', false).then((res) => {
					if (res.code == 1) {
						this.$common.successToShow(res.msg, () => {
							uni.navigateBack()
						})
					}
				})
			},
			// 删除图片
			deletePic(index) {
				this.images.splice(index, 1)
				this.fileList2.splice(index, 1)
			},
			onChooseImg() {
				this.$common.chooseImage({}, (data) => {
					if (data) {
						this.images.push(data)
					}
				}, 9)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.images.length
				lists.map((item) => {
					this.images.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.images[fileListLen]
					this.images.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.uploadImgUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							let datas = JSON.parse(res.data)
							this.fileList2.push(datas.url)
							setTimeout(() => {
								resolve(datas.url)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: rgba(242, 245, 242, 1);
	}

	.content {
		padding-top: 18rpx;

		.baibox {
			background-color: #ffffff;
			padding: 20rpx 30rpx 20rpx;
			margin-bottom: 20rpx;

			.title {
				padding-top: 20rpx;
				font-size: 28rpx;
				font-weight: bold;
				letter-spacing: 0em;
				color: #3D3D3D;
				line-height: 40rpx;

				text {
					font-size: 24rpx;
					font-weight: normal;
					letter-spacing: 0em;
					color: #999999;
					font-weight: 500;
					padding-left: 12rpx;
				}
			}

			.imgView {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.imgList:nth-child(3n) {
					margin-right: 0;
				}

				.imgList {
					position: relative;
					padding-bottom: 10rpx;
					margin-right: 31rpx;

					.imgClose {
						position: absolute;
						top: -8px;
						right: -8px;
						z-index: 1;
						width: 36rpx;
						height: 36rpx;
						background-color: #45C4B0;
						color: #fff;
						line-height: 36rpx;
						text-align: center;
						border-radius: 100px;
					}
				}

				.imgList:not(:first-child) {
					// margin-left: 15rpx;
				}
			}

			.pb20 {
				padding-bottom: 20rpx;
			}

			.put {
				width: 690rpx;
				font-size: 28rpx;
				color: #3d3d3d;
				padding: 8rpx 0 20rpx;
				border-bottom: 1px solid rgba(153, 153, 153, 0.1);
			}

			.area {
				width: 610rpx;
				padding: 20rpx 8rpx;
				max-height: 500rpx;
				min-height: 200rpx;
				resize: none;
				font-size: 28rpx;
				color: #3d3d3d;
			}
		}

		.subbox {
			width: 630rpx;
			height: 86rpx;
			border-radius: 8px;
			opacity: 1;
			background: #45C4B0;
			text-align: center;
			margin: 0 auto;
			line-height: 86rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
	}

	.font-gray {
		color: #999999;
	}

	.order_num {
		width: 690rpx;
		font-size: 28rpx;
		color: #3d3d3d;
		padding: 8rpx 0 20rpx;
		border-bottom: 1px solid rgba(153, 153, 153, 0.1);

		.order_num_right {
			font-family: '思源黑体';
			font-size: 12px;
			color: #999999;
		}
	}
</style>